package vma.nicici.chat.communication;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.DefaultHttpClient;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

public abstract class HttpRequest {
	
	public static final String HTTP_ENCODING = "utf-8";
	
	protected List<? extends NameValuePair> httpParams;
	protected String url;
	
	HttpRequestBase type;

	public HttpRequest(String url, List<? extends NameValuePair> parameters, HttpRequestBase type) {
		this.url = url;
		this.httpParams = parameters;
		this.type = type;
		
		if (!this.url.endsWith("?")) {
			this.url += "?";
		}
	}
	
	public abstract String getStringResponse() throws IOException, URISyntaxException;
	public abstract void clientPreparation(HttpRequestBase type);

	public String decodeUrlWithParameters() {
		return url+URLEncodedUtils.format(httpParams, HTTP_ENCODING);
	}
	
	protected String send(String urlWithParams) throws IOException, URISyntaxException {
		DefaultHttpClient httpClient = new DefaultHttpClient();
		URI uri = new URI(urlWithParams);
		type.setURI(uri);
		
		clientPreparation(type);
		HttpResponse response = httpClient.execute(type);
		
		BufferedReader br = new BufferedReader(
		new InputStreamReader(response.getEntity().getContent()));
		StringBuilder result = new StringBuilder();
		while (true) {
			String line = br.readLine();
			if (line == null) break;
			result.append(line+"\n");
		}
		
		return result.toString();
	}
	
	public void getStringResponseAsynch(final AsynchCallback<String> callback) {
		Thread t = new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					String response;
					response = getStringResponse();
					callback.onFinished(response);
				} catch (Exception e) {
					e.printStackTrace();
				} 
			}
		});
		
		t.start();
	}
	
	
	public Bitmap getBitmapResponse() {
	    try {
	        URL url = new URL( decodeUrlWithParameters() );
	        Log.d("getBitmapResponse", url.toString());
	        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
	        connection.setDoInput(true);
	        connection.connect();
	        InputStream input = connection.getInputStream();
	        Bitmap myBitmap = BitmapFactory.decodeStream(input);
	        return myBitmap;
	    } catch (IOException e) {
	        e.printStackTrace();
	        return null;
	    }
	}
	
	public void getBitmapResponseAsynch(final AsynchCallback<Bitmap> callback) {
		Thread t = new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					Bitmap response;
					response = getBitmapResponse();
					callback.onFinished(response);
				} catch (Exception e) {
					e.printStackTrace();
				} 
			}
		});
		
		t.start();
	}
	
}
